package com.jyw.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * Created by next on 2016/3/24.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    private Context mCtx;
    private static final String name = "city";
    private static final int version = 1;

    public DatabaseHelper(Context context) {
        super(context, name, null, version);
        mCtx = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS recent_city (address integer primary key autoincrement, name varchar(40), date INTEGER)");
        db.execSQL("CREATE TABLE IF NOT EXISTS city (address integer primary key autoincrement, name varchar(40), pinyin varchar(40))");

        BufferedReader breader = null;
        try {
            breader = new BufferedReader(new InputStreamReader(mCtx.getResources().getAssets().open("city.sql")));
            String str0;
            while (null != (str0 = breader.readLine())) {
                db.execSQL(str0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (breader != null) {
                    breader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}
